package com.lori.android.core.util;

import android.app.Activity;

import java.util.Stack;

/**
 * @author lori.lin
 * @date 14-8-26 下午4:00.
 */
public class AppManager {
    private static AppManager instance;
    private static Stack<Activity> activityStack;

    public AppManager getAppManager()
    {
        if(instance==null)
        {
            instance = new AppManager();
        }
        return instance;
    }


    /**
     * 添加activity到栈中
     * @param activity
     */
    public void addActivity(Activity activity)
    {
        if(activityStack==null)
        {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 获取当前activity
     * @return
     */
    public Activity getCurrentActivity()
    {
        Activity activity = null;
        if(activityStack!=null)
        {
            activity = activityStack.lastElement();
        }
        return activity;
    }

    /**
     * 结束当前activity
     */
    public void finishCurrentActivity()
    {
        if(activityStack==null)
        {
            return;
        }
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * 结束指定的activity
     * @param activity
     */
    public void finishActivity(Activity activity)
    {
        if(activityStack==null)
        {
            return;
        }
        activityStack.remove(activity);
        activity.finish();
        activity = null;
    }

    /**
     * 结束指定类名的activity
     * @param c
     */
    public void finishActivity(Class<?> c)
    {
        for(Activity activity:activityStack)
        {
            if(c.equals(activity.getClass()))
            {
                finishActivity(activity);
            }
        }
    }

}
